/**
 * 黑色透明材质修复工具
 * 修复透明材质显示黑色区域的问题
 */

export class BlackTransparencyFixer {
    /**
     * 修复场景中所有黑色透明材质
     * @param {THREE.Scene} scene - Three.js场景对象
     */
    static fixBlackTransparencyInScene(scene) {
        console.log('\n🎨 开始修复黑色透明材质问题...\n');
        
        let fixedCount = 0;
        let totalTransparent = 0;
        
        scene.traverse((obj) => {
            if (obj.isMesh && obj.material) {
                const materials = Array.isArray(obj.material) ? obj.material : [obj.material];
                
                materials.forEach((mat, index) => {
                    // 检查是否为透明材质
                    const isTransparent = mat.transparent || mat.alphaTest > 0 || mat.opacity < 1;
                    
                    if (isTransparent) {
                        totalTransparent++;
                        
                        // 检查是否为黑色
                        const isBlack = mat.color && (
                            mat.color.r === 0 && mat.color.g === 0 && mat.color.b === 0
                        );
                        
                        if (isBlack) {
                            console.log(`🔧 修复黑色透明材质: ${obj.name || '(unnamed)'}`);
                            console.log(`   修复前: color=(${mat.color.r}, ${mat.color.g}, ${mat.color.b})`);
                            
                            // 修复为白色
                            mat.color.setRGB(1, 1, 1);
                            mat.needsUpdate = true;
                            
                            console.log(`   修复后: color=(${mat.color.r}, ${mat.color.g}, ${mat.color.b})`);
                            fixedCount++;
                        }
                    }
                });
            }
        });
        
        console.log(`\n✅ 修复完成！`);
        console.log(`   总透明材质: ${totalTransparent} 个`);
        console.log(`   修复黑色材质: ${fixedCount} 个`);
        
        if (fixedCount > 0) {
            console.log(`\n🎯 建议：重新上传模型以从源头解决此问题`);
        }
        
        return {
            totalTransparent,
            fixedCount
        };
    }
    
    /**
     * 检查但不修复黑色透明材质
     * @param {THREE.Scene} scene - Three.js场景对象
     */
    static checkBlackTransparency(scene) {
        console.log('\n🔍 检查黑色透明材质...\n');
        
        const blackMaterials = [];
        
        scene.traverse((obj) => {
            if (obj.isMesh && obj.material) {
                const materials = Array.isArray(obj.material) ? obj.material : [obj.material];
                
                materials.forEach((mat, index) => {
                    const isTransparent = mat.transparent || mat.alphaTest > 0 || mat.opacity < 1;
                    const isBlack = mat.color && (
                        mat.color.r === 0 && mat.color.g === 0 && mat.color.b === 0
                    );
                    
                    if (isTransparent && isBlack) {
                        blackMaterials.push({
                            meshName: obj.name || '(unnamed)',
                            materialType: mat.type,
                            transparent: mat.transparent,
                            alphaTest: mat.alphaTest,
                            opacity: mat.opacity,
                            position: obj.position.clone()
                        });
                    }
                });
            }
        });
        
        if (blackMaterials.length > 0) {
            console.log(`❌ 发现 ${blackMaterials.length} 个黑色透明材质:`);
            blackMaterials.forEach((item, index) => {
                console.log(`${index + 1}. ${item.meshName}`);
                console.log(`   材质类型: ${item.materialType}`);
                console.log(`   透明设置: transparent=${item.transparent}, alphaTest=${item.alphaTest}`);
                console.log(`   位置: (${item.position.x.toFixed(2)}, ${item.position.y.toFixed(2)}, ${item.position.z.toFixed(2)})`);
                console.log('');
            });
        } else {
            console.log('✅ 未发现黑色透明材质问题');
        }
        
        return blackMaterials;
    }
}

// 创建便捷实例
const blackTransparencyFixer = {
    // 修复黑色透明材质
    fix: () => {
        const scene = window.manager?.getCurrentScene()?.scene;
        if (!scene) {
            console.error('❌ 场景未加载');
            return;
        }
        return BlackTransparencyFixer.fixBlackTransparencyInScene(scene);
    },
    
    // 检查黑色透明材质
    check: () => {
        const scene = window.manager?.getCurrentScene()?.scene;
        if (!scene) {
            console.error('❌ 场景未加载');
            return;
        }
        return BlackTransparencyFixer.checkBlackTransparency(scene);
    }
};

// 暴露到全局
window.blackTransparencyFixer = blackTransparencyFixer;

export default blackTransparencyFixer;
